const javaData = require('./src/data/javadata.json')
const frontData = require('./src/data/frontdata.json')
const bodyParser = require('body-parser')
const fs = require('fs')
const path = require('path')
module.exports = {
    devServer: {
        before(app) {
            app.use(bodyParser.json())
            app.use(bodyParser.urlencoded({extended: true}))
            app.get('/agree/list', (req, res) => {
                let agreeData = fs.readFileSync(path.join(__dirname, 'src/data/agree.json')).toString()
                agreeData = agreeData ? JSON.parse(agreeData) : []
                res.send({
                    code: 1,
                    data: agreeData
                })
            })
            app.get('/check', (req, res) => {
                let { phone, article_id } = req.query
                // 查询是否点过赞
                // 获取所有的点赞数据
                let agreeData = fs.readFileSync(path.join(__dirname, 'src/data/agree.json')).toString()
                agreeData = agreeData ? JSON.parse(agreeData) : []
                // 判断是否点过赞=>筛选一下数据
                let i = 0
                for(i; i< agreeData.length; i++) {
                    if(agreeData[i].article_id == article_id) {
                        // 存在 => 已经点过赞
                        break
                    }
                }
                if(i == agreeData.length ){
                    // 没点过赞
                    res.send({
                        code: 0
                    })
                } else {
                    res.send({
                        code: 1
                    })
                }
            })
            app.get('/index/page', (req, res) => {
                const { type } = req.query
                if(type === 'java') {
                    res.send({
                        code: 1,
                        data: javaData
                    })
                } else {
                    res.send({
                        code: 1,
                        data: frontData
                    })
                }
            })
            
            app.post('/register', (req, res) => {
                const { phone, password } = req.body
                // 注册
                let userData = fs.readFileSync(path.join(__dirname, 'src/data/user.json')).toString()
                userData = userData ? JSON.parse(userData) : []

                // 先判断手机号是否被占用
                if(userData.some(item => item.phone == phone)) {
                    // 代表已经注册过了
                    res.send({
                        code: 0,
                        msg: '手机号已被注册'
                    })
                } else {
                    // 没有注册过
                    userData.push({
                        phone,
                        password
                    })
                    fs.writeFileSync(path.join(__dirname, 'src/data/user.json'), JSON.stringify(userData))
                    res.send({
                        code: 1,
                        msg: '注册成功'
                    })
                }
            })

            app.post('/login', (req, res) => {
                const { phone, password } = req.body
                let userData = fs.readFileSync(path.join(__dirname, 'src/data/user.json')).toString()
                userData = userData ? JSON.parse(userData) : []

                if(userData.some(item => item.phone == phone && item.password == password)) {
                    res.send({
                        code: 1,
                        msg: '登陆成功'
                    })
                } else{
                    res.send({
                        code: 0,
                        msg: '登录失败'
                    })
                }
            })
            app.post('/agree', (req, res) => {
                const { phone, article_id }  = req.body
                // 获取所有的点赞数据
                let agreeData = fs.readFileSync(path.join(__dirname, 'src/data/agree.json')).toString()
                agreeData = agreeData ? JSON.parse(agreeData) : []
                // 判断是否点过赞=>筛选一下数据
                let i = 0
                for(i; i< agreeData.length; i++) {
                    if(agreeData[i].article_id == article_id && agreeData[i].phone == phone) {
                        // 存在 => 已经点过赞
                        break
                    }
                }
                if(i == agreeData.length ){
                    // 没有点赞
                    agreeData.push({
                        id: agreeData.length + 1,
                        phone: phone,
                        article_id: article_id
                    })
                    // 写入文件
                    fs.writeFileSync(path.join(__dirname, 'src/data/agree.json'), JSON.stringify(agreeData))
                    res.send({
                        code: 1,
                        msg: '点赞成功'
                    })
                } else {
                    // 点过赞，取消点赞
                    agreeData.splice(i, 1)
                    fs.writeFileSync(path.join(__dirname, 'src/data/agree.json'), JSON.stringify(agreeData))
                    res.send({
                        code: 0,
                        msg: '取消点赞成功'
                    })
                }
            })

        }
    }
}